// =====================================================================================
// 
//       Filename:  logger.h
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  03/25/2010 11:14:09 AM
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  tiger lee (tiger), tiger.lee@taodinet.com
//        Company:  Hangzhou TaodiNet Ltd.
// 
// =====================================================================================

#ifndef  LOGGER_H_INC
#define  LOGGER_H_INC
#pragma  once

#include	"utils.h"
#include    <stdio.h>
#include	<time.h>


#ifndef __WIN32__
#define __FUNCTION__ __PRETTY_FUNCTION__
#endif

#ifdef FILE_LOG
extern FILE* logger_fd;
void open_log(const char* path);
void close_log();

#define FILE_LOG_START(LogFile) atexit(close_log); open_log(LogFile)
#define LOGGER_FD logger_fd
#else
#define LOGGER_FD stderr
#define FILE_LOG_START(...) do { } while(0)
#endif

#define LOG(fmt,  ...)   fprintf(LOGGER_FD, fmt, ##__VA_ARGS__)
#define LOG_INFO(fmt,  ...)   fprintf(LOGGER_FD, "[%s][INFO][%s:%d] "fmt,  lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG_WARN(fmt,  ...)   fprintf(LOGGER_FD, "[%s][WARN][%s:%d] "fmt,  lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG_ERR(fmt,   ...)   fprintf(LOGGER_FD, "[%s][ERR][%s:%d] "fmt,   lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__, ##__VA_ARGS__)

#ifdef DEBUG
#define LOG_DEBUG(fmt, ...)   fprintf(LOGGER_FD, "[%s][DEBUG][%s:%d] "fmt, lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__, ##__VA_ARGS__)
#define LOG_TRACE(fmt, ...)   fprintf(LOGGER_FD, "[%s][TRACE][%s:%d] "fmt, lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define LOG_DEBUG(...) do { } while(0)
#define LOG_TRACE(...) do { } while(0)
#endif

#define LOG_ETERM(term) fprintf(LOGGER_FD, "[%s][ETERM][%s:%d] ", lk_clock::now("%Y-%m-%d %H:%M:%S"), __FUNCTION__, __LINE__); erl_print_term(LOGGER_FD, term); fprintf(LOGGER_FD, "\r\n")

#endif   // ----- #ifndef LOGGER_H_INC  -----

